package org.vectrics.recruiter.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vectrics.SystemException;
import org.vectrics.company.Company;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.struts.HttpRequestConverter;
import org.vectrics.struts.Request;
import org.vectrics.struts.UserBufferManager;
import org.vectrics.user.Person;

public class BufferRemoveAction extends Action {

	private transient final Logger log = Logger.getLogger(this.getClass());
    
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		
		Request request = HttpRequestConverter.convertRequest(req);
		
		UserBufferManager bufferManager = new UserBufferManager();	
		log.debug("execute() called, reading http parameters");
	    Long recordId = new Long(request.getParameter("recordId"));

	    String objectType = request.getParameter("objectType");
		if (objectType.equals(Job.OBJECT_TYPE)) {
		    Job job = RecruitServiceLocator.getJobProvider().findJob(recordId);
		    bufferManager.removeBufferedObject(request, job);
		} else if (objectType.equals(Person.OBJECT_TYPE)) {
		    Person person = CoreServiceLocator.getPersonService().findPersonById(recordId);
		    bufferManager.removeBufferedObject(request, person);
		} else if (objectType.equals(Company.OBJECT_TYPE)) {
		    Company company = CoreServiceLocator.getCompanyService().findCompany(recordId);
		    bufferManager.removeBufferedObject(request, company);
		} else {
		    throw new SystemException("Do not know how to handle buffer object: " + objectType);
		}

		String original_request = request.getParameter("original_request");
		ActionForward forward = new ActionForward(original_request,true);
		return forward;
	}

}